/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:28
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2022-08-03 10:40:05
 */
package com.wxhandle.cleandemo.application.dto.identity;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.wxhandle.cleandemo.core.domain.base.QueryFilter;
import com.wxhandle.cleandemo.core.domain.identity.SysClaim;
import com.wxhandle.cleandemo.core.domain.identity.SysService.ClaimType;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import lombok.Data;

@Data
public class SysClaimParam implements QueryFilter<SysClaim> {

    private ClaimType type;

    @Override
    public Specification<SysClaim> getSpecification() {
        Specification<SysClaim> specification = new Specification<SysClaim>(){

            @Override
            public Predicate toPredicate(Root<SysClaim> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate pc = criteriaBuilder.conjunction();
                List<Expression<Boolean>> es=pc.getExpressions();
                if(null != type)
                    es.add(criteriaBuilder.equal(root.get("type"), getType().ordinal()));
                return pc;
            }

        };
        return specification;
    }

    @Override
    public Sort getSort() {
        return Sort.by(Sort.Direction.ASC, "sorting");
    }

}
